package com.software.controller;


import com.software.base.ResultInfo;
import com.software.dto.AddObserverDto;
import com.software.dto.ReplyDto;
import com.software.service.ObserverService;
import com.software.utils.AssertUtil;
import com.software.vo.ObserverVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wyj
 * @since 2024-04-03
 */
@Controller
@RequestMapping("/observer")
public class ObserverController {

    @Autowired
    private ObserverService observerService;

    /**
     * @description:添加留言
     * @author: wyj
     * @date: 2024/4/3 20:33
     * @param:
     * @return:
     */
    @PostMapping("/front/postObserver")
    @ResponseBody
    public ResultInfo postObserver(@RequestBody AddObserverDto observerDto) {
        System.out.println(observerDto);
        Integer result = observerService.addObserver(observerDto);
        AssertUtil.isTrue(result != 1, "添加失败");
        return ResultInfo.success();
    }

    /**
     * @description:获取所有的留言列表
     * @author: wyj
     * @date: 2024/4/4 11:43
     * @param:
     * @return:
     */
    @GetMapping("/front/observerList")
    @ResponseBody
    public ResultInfo observerList(@RequestParam("productId") Integer productId) {
        List<ObserverVo> observerList = observerService.getObserverList(productId);
        return ResultInfo.success(observerList);
    }

    /**
     * @description:回复留言
     * @author: wyj
     * @date: 2024/4/4 11:43
     * @param:
     * @return:
     */
    @PostMapping("/front/postComment")
    @ResponseBody
    public ResultInfo postComment(@RequestBody  ReplyDto replyDto) {
        Integer result = observerService.addComment(replyDto);
        AssertUtil.isTrue(result != 1, "添加回复失败");
        return ResultInfo.success();
    }


    /** @description:删除根留言
      * @author: wyj
      * @date: 2024/4/23 13:50
      * @param:
      * @return:
      */
    @DeleteMapping("/front/deleteRootObserver/{observerId}")
    @ResponseBody
    public ResultInfo deleteRootObserver(@PathVariable("observerId") Integer observerId) {
        System.out.println(observerId);
        Integer result = observerService.deleteRootObserver(observerId);
        AssertUtil.isTrue(result != 1, "删除失败");
        return ResultInfo.success();
    }
}
